<?php
class main extends spController
{
	function index(){
		$this->groupList = spClass('db_contact')->getAllGroups();//print_r($this->groupList);
		$this->display("index.html");
	}
	
	function detail(){
		$this->userList = spClass('db_detail')->findAll(array("group_id" => $this->spArgs('group'), "del_flag" => '0'));//print_r($this->userList);
		$this->groupInfo = spClass("db_contact")->find(array("id" => $this->spArgs('group')));//print_r($this->groupInfo);
		$this->display("group_detail.html");
	}
	
	function addGroup(){
		$id = spClass('db_contact')->create(array(  
				'group_name' => $this->spArgs('groupName'),
				'password' => md5($this->spArgs('inputPassword')),
				'time' => date('Y-m-d H:i:s'),
						));
		spClass('db_detail')->create(
				array(  'group_id' => $id,
						'name' => $this->spArgs('creator'),
					    'tel' => $this->spArgs('tel'),
						'qq' => $this->spArgs('qq'),
						'email' => $this->spArgs('email'),
						'other' => $this->spArgs('other'),
						'time' => date('Y-m-d H:i:s'),
						'updateTime' => date('Y-m-d H:i:s'),
						'is_creator' => '1',
				));
	}
	
	function check(){
		//echo $this->spArgs('entrypassword');
		$group = $this->spArgs('groupid');
		$result = spClass('db_contact')->find(array('id' => $group));//print_r($result);
		if(md5($this->spArgs('entrypassword')) == $result['password']){
			echo "<script>location.href = '/detail/group/{$group}';</script>";
		}else{
			echo "<script>alert('密码不正确，不能进入哦！');</script>";
		}
		
	}
	
	function delUser(){
		try{
			spClass('db_detail')->update(array('id' => $this->spArgs('id')), array('updateTime' => date('Y-m-d H:i:s'), 'del_flag' => 1));
			echo "yes";
		}catch(Exception $e){
			echo $e->getMessage();
		}
	}
	
	function addUser(){
		try{
			if($this->spArgs('contact_id')){
				//修改
				spClass("db_detail")->update(array('id' => $this->spArgs('contact_id'),
						'group_id' => $this->spArgs('group_id')),
						array('name' => $this->spArgs('name'),
								'tel' => $this->spArgs('tel'),
								'qq' => $this->spArgs('qq'),
								'email' => $this->spArgs('email'),
								'other' => $this->spArgs('other'),
								'updateTime' => date('Y-m-d H:i:s'),)
				);
			}else{
				//添加
				spClass("db_detail")->create(array('group_id' => $this->spArgs('group_id'),
						'name' => $this->spArgs('name'),
						'tel' => $this->spArgs('tel'),
						'qq' => $this->spArgs('qq'),
						'email' => $this->spArgs('email'),
						'other' => $this->spArgs('other'),
						'time' => date('Y-m-d H:i:s'),
						'updateTime' => date('Y-m-d H:i:s'),
						'is_creator' => '0',
						'del_flag' => '0'));
			}
			echo 'yes';
		}catch(Exception $e){
			echo $e->getMessage();
		}		
	}
}